package mysgoog.board;

import mysgoog.gtp.*;

public class Worm extends PosSet
{
    private static final long serialVersionUID = 1L;
    private final Board board;
    public final Color color;

    public Worm(Board board, Color color)
    {
        this.board = board;
        this.color = color;
    }

    private PosSet libs = new PosSet();
    private int tsn = -1;

    public PosSet getLibs()
    {
        if (tsn < board.getTSN())
        {
            tsn = board.getTSN();
            libs.clear();
            for (Integer pos : this)
                for (Integer neib : board.getAdjacent(pos))
                    if (board.getStone(neib) == Color.EMPTY)
                        libs.add(neib);
        }
        return libs;
    }
    @Override
    public Worm clone()
    {
        Worm res = new Worm(board, color);
        res.addAll(this);
        if (this.libs != null)
            res.libs = (PosSet) this.libs.clone();
        return res;
    }
    public String toString()
    {
        String res = "";
        for (Integer pos : this)
            res += new Vertex(pos).toString();
        return res;
    }
}
